home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_11 / saks / strtst7.cpp < prev   
Encoding:
C/C++ Source or Header  |  1994-09-08  |  1.6 KB  |  79 lines

  1. Listing 2 - a test program for generic queues based on the generic.h
  2. macros
  3.  
  4. //
  5. // strtst7.cpp - test gueue(str)
  6. //
  7.  
  8. #include <iostream.h>
  9.  
  10. #include "queue7.h"
  11. #include "showheap.h"
  12. #include "str.h"
  13.  
  14. declare(queue, str)
  15.  
  16. ostream &operator<<(ostream &os, queue(str) &q)
  17.     {
  18.     str *ps;
  19.     queue(str)::iterator sqi(q);
  20.     while ((ps = sqi.next()) != 0)
  21.             os << ' ' << *ps;
  22.     return os;
  23.     }
  24.  
  25. #define DIM(a) (sizeof(a)/sizeof(a[0]))
  26.  
  27. void test()
  28.     {
  29.     char c;
  30.     size_t qn;
  31.     str qe;
  32.     queue(str) q[4];
  33.     while (cin >> c)
  34.         {
  35.         showheap();
  36.         if (c == 'q')
  37.             break;
  38.         if (c == 'a')
  39.             {
  40.             cin >> qn >> qe;
  41.             if (qn >= DIM(q))
  42.                 cout << "no such queue\n";
  43.             else
  44.                 q[qn].append(qe);
  45.             }
  46.         else if (c == 'c')
  47.             {
  48.             cin >> qn;
  49.             if (qn >= DIM(q))
  50.                 cout << "no such queue\n";
  51.             else
  52.                 q[qn].clear();
  53.             }
  54.         else if (c == 'r')
  55.             {
  56.             cin >> qn;
  57.             if (qn >= DIM(q))
  58.                 cout << "no such queue\n";
  59.             else if (q[qn].remove(qe))
  60.                 cout << "removed " << qe << '\n';
  61.             else
  62.                 cout << "q[" << qn << "] is empty\n";
  63.             }
  64.         else
  65.             continue;
  66.         for (size_t i = 0; i < DIM(q); ++i)
  67.             cout << i << ':' << q[i] << '\n';
  68.         }
  69.     }
  70.  
  71. int main()
  72.     {
  73.     showheap();
  74.     test();
  75.     showheap();
  76.     return 0;
  77.     }
  78.  
  79.